<?php

namespace app\admin\controller;

use think\captcha\facade\Captcha;
use app\BaseController;
use think\facade\View;
use think\facade\Session;
use think\facade\Config;
use app\common\model\SystemAdmin;
use app\common\controller\Handle;

class Login extends BaseController
{
    // 登录界面
    public function Index()
    {
        if(session('?admin_user') && cache('admin_user'.session('admin_user'))){
            redirect("/Index/Index")->send();
            exit;
        }else{
            $s_num = SystemAdmin::where(['is_supper' => 1, 'status' => 1])->count();
            View::assign('s_num', $s_num);
            View::assign('sysconfig', config('sysconfig'));
            $id = session('admin_user');
            View::assign('admin_user', cache('admin_user' . $id));
            View::assign('login_type', cookie('login_type') ?? 1);
            return View::fetch();
        }
        
    }
    // 验证码
    public function createYzm()
    {
        return Captcha::create();
    }
    public function codeLogin()
    {
        $post = $this->request->post();
        $code = $post['code'];
        if (cache($code)) {
            $admin = SystemAdmin::where(['id' => cache($code)])->find();
            // 存入
            Session::set('admin_user', $admin->id);
            cookie('login_type', 2);
            cache('admin_user' . $admin->id, $admin);
            return json(array('code' => '1', 'msg' => '登录成功!', 'url' => '/Index'));
        } else {
            return json(array('code' => '0', 'msg' => '参数错误!'));
        }
    }
    // 登录
    public function login()
    {
        if (request()->IsAjax()) {
            $post = $this->request->post();

            if (Config::get('sysconfig')['captcha']) {
                if (!isset($post['captcha']) || empty($post['captcha'])) {
                    return json(array('code' => '0', 'msg' => '请输入验证码!'));
                }
                if (!Captcha::check($post['captcha'])) {
                    return json(array('code' => '0', 'msg' => '验证码错误!'));
                }
            }

            // 判断是否含有超管
            $s_num = SystemAdmin::where(['is_supper' => 1, 'status' => 1])->count();
            if ($s_num == 0) {
                $add_admin = [];
                $add_admin['admin'] = trim($post['username']);
                $add_admin['password'] = md5(trim($post['password']));
                $add_admin['status'] = 1;
                $add_admin['add_time'] = time();
                $add_admin['is_supper'] = 1;
                $admin_user = SystemAdmin::create($add_admin);
                // 存入
                Session::set('admin_user', $admin_user->id);
                cookie('login_type', 1);
                cache('admin_user' . $admin_user->id, $add_admin);
                return json(array('code' => '1', 'msg' => '登录成功!', 'url' => '/Index'));
            } else {
                // 判断密码登录
                $admin = SystemAdmin::where(['admin' => trim($post['username'])])->find();
                if (!$admin) {
                    return json(array('code' => '0', 'msg' => '账号或密码错误!'));
                }
                $pwd = md5(trim($post['password']));
                if ($pwd != $admin['password']) {
                    return json(array('code' => '0', 'msg' => '账号或密码错误!'));
                }
                // 存入
                Session::set('admin_user', $admin->id);
                cookie('login_type', 1);
                cache('admin_user' . $admin->id, $admin);
                return json(array('code' => '1', 'msg' => '登录成功!', 'url' => '/Index'));
            }
        }
    }
    // 退出登录
    public function loginOut()
    {
        $id = session('admin_user');
        session('admin_user', null);
        cache('admin_user' . $id, null);
        return json(array('code' => '1', 'msg' => '退出成功!'));
    }

    // 获取登录验证码
    public function getQrcode()
    {
        header('content-type:image/jpeg');
        $get = $this->request->get();
        $cid = $get['cid'];
        if (strlen($cid) != 32) {
            echo 'error';
            exit;
        } elseif (!cache('adminlogin' . $cid)) {
            echo 'error';
            exit;
        }
        $hand = new Handle();
        $data = [
            'scene' => $cid,
            'page' => 'pages/admin_login/admin_login',
            // 'check_path' => false,
            // 'env_version' => 'trial'//体验
            'env_version' => 'release'
        ];
        $res = $hand->getQrCode($data);
        echo $res;
        exit;
    }
}
